package com.hhkj.study.api.order.service.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import com.hhkj.study.admin.order.model.podo.OrderItemPo;
import com.hhkj.study.admin.order.model.res.OrderItemRes;
import com.hhkj.study.admin.order.service.base.OrderItemService;
import com.hhkj.study.admin.order.service.biz.OrderItemBizService;
import com.hhkj.study.api.order.model.req.ApiUserOrderListReq;
import com.hhkj.study.api.order.model.res.ApiUserOrderRes;
import com.hhkj.study.api.order.service.biz.ApiOrderBizService;
import com.hhkj.study.api.user.common.checker.AccountChecker;
import com.hhkj.study.api.user.model.res.ApiCustomerUserBaseRes;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * @Author luowu
 * @Date 2024/10/21 涵 9:06:04
 */
@Service
public class ApiOrderBizServiceImpl implements ApiOrderBizService {

    @Resource
    private OrderItemBizService orderItemBizService;

    @Resource
    private AccountChecker accountChecker;

    @Override
    public List<ApiUserOrderRes> list(ApiUserOrderListReq req) {
        // 校验账号是否合法
        ApiCustomerUserBaseRes currentCustomerUser = accountChecker.checkAccountValid();
        req.setBuyerId(currentCustomerUser.getId());
        List<OrderItemRes> orderItemList = orderItemBizService.list(req);
        if (CollectionUtil.isEmpty(orderItemList)) {
            return Lists.newArrayList();
        }
        List<ApiUserOrderRes> userOrderList =  Lists.newArrayList();
        Map<Long, List<OrderItemRes>> orderItemMap = orderItemList.stream().collect(Collectors.groupingBy(OrderItemPo::getOrderId, () -> new TreeMap<>(Comparator.reverseOrder()), Collectors.toList()));
        orderItemMap.forEach((orderId, orderItemInnerList) -> {
            if (CollectionUtil.isNotEmpty(orderItemInnerList)) {
                OrderItemPo firstOrderItem = CollectionUtil.getFirst(orderItemInnerList);
                ApiUserOrderRes apiUserOrderRes = new ApiUserOrderRes();
                apiUserOrderRes.setOrderId(firstOrderItem.getOrderId());
                apiUserOrderRes.setOrderCode(firstOrderItem.getOrderCode());
                apiUserOrderRes.setOrderStatus(firstOrderItem.getOrderStatus());
                apiUserOrderRes.setOrderCreateTime(firstOrderItem.getOrderCreateTime());
                apiUserOrderRes.setOrderPayPrice(firstOrderItem.getOrderPayPrice());
                apiUserOrderRes.setOrderTotalPrice(firstOrderItem.getOrderTotalPrice());
                apiUserOrderRes.setOrderItemList(orderItemInnerList);
                userOrderList.add(apiUserOrderRes);
            }
        });
        return userOrderList;
    }

    @Override
    public ApiUserOrderRes get(Long orderItemId) {
        ApiUserOrderListReq req = new ApiUserOrderListReq();
        req.setId(orderItemId);
        List<ApiUserOrderRes> orderList = list(req);
        if (CollectionUtil.isEmpty(orderList)) {
            return null;
        }
        return CollectionUtil.getFirst(orderList);
    }
}
